<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="banner标题">
        <el-input v-model="banner.title" style="width: 400px"/>
      </el-form-item>

      <el-form-item label="封面">

        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'eduoss/fileoss?host=banner'"
          class="avatar-uploader">
          <img :src="banner.imageUrl" style="max-width: 1200px">
        </el-upload>

      </el-form-item>
      <el-form-item label="链接地址">
        <el-input v-model="banner.linkUrl" controls-position="right" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="banner.sort" controls-position="right" :min="0" :max="100"/>
      </el-form-item>


      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import banner from '@/api/cms/banner'

  const defaultForm = {
    title: '',
    imageUrl:  process.env.OSS_PATH+"/banner/banner.png",
    linkUrl: '',
    sort: ''
  }

  export default {
    data() {
      return {
        banner: defaultForm
        , BASE_API: process.env.BASE_API//获取dev.env.js中的BASE_API
        , saveBtnDisabled: false // 保存按钮是否禁用,

      }
    }, watch: {
      //监听路由变化
      $route(to, from) {
        console.log('watch $route')
        this.init()
      }
    }, created() {
      console.log(this.BASE_API)
      this.init()
    }, methods: {
      init() {
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id
          console.log(id)
          this.getDataById(id)
        } else {
          // 使用对象拓展运算符，拷贝对象，而不是引用，
          // 否则新增一条记录后，defaultForm就变成了之前新增的banner的值
          this.banner = { ...defaultForm }
        }
      },
      getDataById(id) {
        banner.getById(id).then(response => {
          this.banner = response.data.item
          console.log(response.data.item)
        }).catch((response) => {
          this.$message.error('获取数据失败')
        })
      },
      saveOrUpdate() {
        this.saveBtnDisabled = true
        if (!this.banner.id) {
          this.saveData()
        } else {
          this.updateData()
        }
      },
      // 保存
      saveData() {
        banner.save(this.banner).then(response => {
          return this.$message.success('保存成功!')
        }).then(() => {
          this.$router.push({ path: '/cms' })
        }).catch((response) => {
          // console.log(response)
          this.$message.error('保存失败')
        })
      },
      // 根据id更新记录
      updateData() {
        this.saveBtnDisabled = true
        banner.updateById(this.banner).then(response => {
          return this.$message.success('修改成功!')
        }).then(() => {
          this.$router.push({ path: '/cms' })
        }).catch((response) => {
          this.$message.error('保存失败')
        })
      },
      //5. 上传封面成功调用的方法
      handleAvatarSuccess(res, file) {
        this.banner.imageUrl = res.data.url

      },
      //6. 上传之前调用的方法 约定文件类型
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>

<style scoped>

</style>
